package com.ruoyi.produce.process.scheduling.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.produce.base.bom.domain.ProductionMachine;
import com.ruoyi.produce.process.scheduling.domain.MachineScheduling;
import com.ruoyi.produce.process.scheduling.service.IMachineSchedulingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/produce/machineScheduling")
@Api(value = "设备排产模块", tags = "设备排产接口")
public class MachineSchedulingController extends BaseController {
    @Autowired
    private IMachineSchedulingService machineSchedulingService;

    /**
     * 获取设备排产记录
     *
     * @param machineScheduling
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("获取设备排产记录")
    public TableDataInfo list(@RequestBody MachineScheduling machineScheduling) {
        startPage();

        List<MachineScheduling> machineSchedulingList = machineSchedulingService.selectMachineSchedulingList(machineScheduling);

        return getDataTable(machineSchedulingList);
    }

    /**
     * 根据设备编号获取设备排产记录
     *
     * @param machineId
     * @return
     */
    @GetMapping("/list/{machineId}")
    @ApiOperation("根据设备编号获取设备排产记录")
    public TableDataInfo selectMachineSchedulingListByMachineId(@PathVariable String machineId) {
        startPage();

        List<MachineScheduling> machineSchedulingList = machineSchedulingService.selectMachineSchedulingListByMachineId(machineId);

        return getDataTable(machineSchedulingList);
    }

//    /**
//     * 新增设备排产记录
//     *
//     * @param machineScheduling
//     * @return
//     */
//    @PostMapping("/add")
//    @ApiOperation("新增设备排产记录")
//    public AjaxResult add(@RequestBody MachineScheduling machineScheduling) {
//        return toAjax(machineSchedulingService.insertMachineScheduling(machineScheduling));
//    }

    /**
     * 修改设备排产记录
     *
     * @param machineScheduling
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("修改设备排产记录")
    public AjaxResult edit(@RequestBody MachineScheduling machineScheduling) {
        return toAjax(machineSchedulingService.updateMachineScheduling(machineScheduling));
    }

    /**
     * 删除设备排产记录
     *
     * @param machineScheduling
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation("删除设备排产记录")
    public AjaxResult delete(@RequestBody MachineScheduling machineScheduling) {

        return toAjax(machineSchedulingService.deleteMachineScheduling(machineScheduling));
    }

    /**
     * 获取计划自动排产
     *
     * @param worksheetIds
     * @return
     */
    @PostMapping("/automatic")
    @ApiOperation("计划排产，可修改")
    public AjaxResult automaticScheduling(@RequestParam String[] worksheetIds) {
        List<MachineScheduling> machineSchedulingList = machineSchedulingService.automaticScheduling(worksheetIds);
        if (machineSchedulingList == null) {
            return AjaxResult.error("产品结构不存在设备关联，请添加设备再重新获取排产计划");
        }
        return AjaxResult.success(machineSchedulingList);
    }

    /**
     * 确认排产
     *
     * @param machineSchedulingList
     * @return
     */
    @PostMapping("/confirm")
    @ApiOperation("确认排产")
    public AjaxResult confirmScheduling(@RequestBody List<MachineScheduling> machineSchedulingList) {
        return toAjax(machineSchedulingService.confirmScheduling(machineSchedulingList));
    }

    /**
     * 根据加工单编号查询该加工单可以下放的设备
     *
     * @param worksheetId
     * @return
     */
    @GetMapping("/searchMachineByWorkSheetId")
    @ApiOperation("根据加工单编号查询该加工单可以下放的设备")
    public AjaxResult searchMachineByWorkSheetId(@RequestParam String worksheetId) {
        List<ProductionMachine> deviceList = machineSchedulingService.searchMachineByWorkSheetId(worksheetId);
        if (deviceList.isEmpty()) {
            return AjaxResult.error("产品结构不存在设备关联，请添加设备再重新获取排产计划");
        }
        return AjaxResult.success(deviceList);
    }
}
